package com.qeeyou.apps.android.craftsman.service;

import com.google.gson.Gson;
import com.qeeyou.apps.android.craftsman.po.CommonInfo;
import com.qeeyou.apps.android.craftsman.po.LoginInfo;
import com.qeeyou.apps.android.craftsman.po_wrap.LoginResultInfo;
import com.qeeyou.apps.android.craftsman.po.RegisterInfo;
import com.qeeyou.apps.android.craftsman.po_wrap.RegisterResultInfo;
import com.qeeyou.apps.android.craftsman.po.UserAddressInfo;
import com.qeeyou.apps.android.craftsman.po_wrap.UploadImgResultInfo;
import com.qeeyou.apps.android.craftsman.po_wrap.UserAddressResultInfo;
import com.qeeyou.apps.android.craftsman.po.UserInfo;
import com.qeeyou.apps.android.craftsman.po.UserResultInfo;
import com.qeeyou.apps.android.craftsman.utils.OKHttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xuchengxiong on 2017/12/25.
 */

public class UserService {

    private static UserService instance;
    public static UserService getInstance() {
        if(instance == null) {
            instance = new UserService();
        }
        return instance;
    }
    private UserService() {

    }

    //用户注册
    private String userRegisterApiUrl() {
        return APIConfig.BASE_URL + "/login/register";
    };

    //用户登录
    private String userLoginApiUrl() {
        return APIConfig.BASE_URL + "/login/loginIn";
    };

    //退出登录
    private String userLogoutApiUrl() {
        return APIConfig.BASE_URL + "/login/loginOut";
    }

    //修改密码
    private String userModifyPasswordApiUrl() {
        return APIConfig.BASE_URL + "/my/modifyPassWord";
    }

    //获取用户信息
    private String userInfoApiUrl() {
        return APIConfig.BASE_URL + "/my/personalData";
    }

    //修改用户信息
    private String modifyUserInfoApiUrl() {
        return APIConfig.BASE_URL + "/my/replacePersonalData";
    }

    //获取用户地址
    private String userAddressListApiUrl() {
        return APIConfig.BASE_URL + "/my/getListByUserId";
    }

    //保存地址
    private String modifyUserAddressApiUrl() {
        return APIConfig.BASE_URL + "/my/saveAddress";
    }

    //用户注册
    public RegisterInfo userRegister(Map<String, String> params) {
        Map<String, String> paramMap = APIConfig.getHeaderPar() != null ? (Map<String, String>)APIConfig.getHeaderPar().clone(): new LinkedHashMap<String, String>();
        //TODO -- 添加参数
        if(params != null && params.size() > 0) {
            paramMap.putAll(params);
        }
        String returnString = null;
        if(OKHttpUtil.isNetworkAvailable(SystemContext.getInstance().getContext())){
            try {
                String urlStr = userRegisterApiUrl();
                returnString = OKHttpUtil.downWithPostJsonMethod(urlStr, paramMap, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //若返回为null,则是网络错误
        if(returnString == null || returnString.equalsIgnoreCase("")) {
            return null;
        } else {
            RegisterResultInfo resultInfo = null;
            try {
                resultInfo = new Gson().fromJson(returnString, RegisterResultInfo.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(resultInfo == null || CommonErrorHandler.getInstance().isError(resultInfo.getStatus(), resultInfo.getMessage())) {
                return null;
            } else {
                RegisterInfo info = resultInfo.getResponse();
                return info;
            }
        }
    }

    //用户登录
    public LoginInfo userLogin(Map<String, String> params) {
        Map<String, String> paramMap = APIConfig.getHeaderPar() != null ? (Map<String, String>)APIConfig.getHeaderPar().clone(): new LinkedHashMap<String, String>();
        //TODO -- 添加参数
        if(params != null && params.size() > 0) {
            paramMap.putAll(params);
        }
        String returnString = null;
        if(OKHttpUtil.isNetworkAvailable(SystemContext.getInstance().getContext())){
            try {
                String urlStr = userLoginApiUrl();
                returnString = OKHttpUtil.downWithPostJsonMethod(urlStr, paramMap, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //若返回为null,则是网络错误
        if(returnString == null || returnString.equalsIgnoreCase("")) {
            return null;
        } else {
            LoginResultInfo resultInfo = null;
            try {
                resultInfo = new Gson().fromJson(returnString, LoginResultInfo.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(resultInfo == null || CommonErrorHandler.getInstance().isError(resultInfo.getStatus(), resultInfo.getMessage())) {
                return null;
            } else {
                LoginInfo info = resultInfo.getResponse();
                return info;
            }
        }
    }

    //退出登录
    public CommonInfo userLogout(Map<String, String> params) {
        Map<String, String> paramMap = APIConfig.getHeaderPar() != null ? (Map<String, String>)APIConfig.getHeaderPar().clone(): new LinkedHashMap<String, String>();
        //TODO -- 添加参数
        if(params != null && params.size() > 0) {
            paramMap.putAll(params);
        }

        Map<String, String> headerMap = new HashMap<>();
        String token = SystemContext.getInstance().getToken();
        String userId = SystemContext.getInstance().getUserId();
        headerMap.put("token", token);
        headerMap.put("userid", userId);

        String returnString = null;
        if(OKHttpUtil.isNetworkAvailable(SystemContext.getInstance().getContext())){
            try {
                String urlStr = userLogoutApiUrl();
                returnString = OKHttpUtil.downWithPostMethod(urlStr, paramMap, headerMap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //若返回为null,则是网络错误
        if(returnString == null || returnString.equalsIgnoreCase("")) {
            return null;
        } else {
            CommonInfo resultInfo = null;
            try {
                resultInfo = new Gson().fromJson(returnString, CommonInfo.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(resultInfo == null || CommonErrorHandler.getInstance().isError(resultInfo.getStatus(), resultInfo.getMessage())) {
                return null;
            } else {
                return resultInfo;
            }
        }
    }

    //修改密码
    public CommonInfo userModifyPassword(Map<String, String> params) {
        Map<String, String> paramMap = APIConfig.getHeaderPar() != null ? (Map<String, String>)APIConfig.getHeaderPar().clone(): new LinkedHashMap<String, String>();
        //TODO -- 添加参数
        if(params != null && params.size() > 0) {
            paramMap.putAll(params);
        }
        Map<String, String> headerMap = new HashMap<>();
        String token = SystemContext.getInstance().getToken();
        String userId = SystemContext.getInstance().getUserId();
        headerMap.put("token", token);
        headerMap.put("userid", userId);

        String returnString = null;
        if(OKHttpUtil.isNetworkAvailable(SystemContext.getInstance().getContext())){
            try {
                String urlStr = userModifyPasswordApiUrl();
                returnString = OKHttpUtil.downWithPostMethod(urlStr, paramMap, headerMap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //若返回为null,则是网络错误
        if(returnString == null || returnString.equalsIgnoreCase("")) {
            return null;
        } else {
            CommonInfo resultInfo = null;
            try {
                resultInfo = new Gson().fromJson(returnString, CommonInfo.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(resultInfo == null || CommonErrorHandler.getInstance().isError(resultInfo.getStatus(), resultInfo.getMessage())) {
                return null;
            } else {
                return resultInfo;
            }
        }
    }

    //获取个人信息
    public UserInfo getUserInfo(Map<String, String> params) {
        Map<String, String> paramMap = APIConfig.getHeaderPar() != null ? (Map<String, String>)APIConfig.getHeaderPar().clone(): new LinkedHashMap<String, String>();
        //TODO -- 添加参数
        if(params != null && params.size() > 0) {
            paramMap.putAll(params);
        }
        Map<String, String> headerMap = new HashMap<>();
        String token = SystemContext.getInstance().getToken();
        String userId = SystemContext.getInstance().getUserId();
        headerMap.put("token", token);
        headerMap.put("userid", userId);

        String returnString = null;
        if(OKHttpUtil.isNetworkAvailable(SystemContext.getInstance().getContext())){
            try {
                String urlStr = userInfoApiUrl();
                returnString = OKHttpUtil.downWithGetMethod(urlStr, paramMap, headerMap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //若返回为null,则是网络错误
        if(returnString == null || returnString.equalsIgnoreCase("")) {
            return null;
        } else {
            UserResultInfo resultInfo = null;
            try {
                resultInfo = new Gson().fromJson(returnString, UserResultInfo.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(resultInfo == null || resultInfo.getResponse() == null || CommonErrorHandler.getInstance().isError(resultInfo.getStatus(), resultInfo.getMessage())) {
                return null;
            } else {
                return resultInfo.getResponse();
            }
        }
    }

    //修改个人资料
    public CommonInfo modifyUserInfo(Map<String, String> params) {
        Map<String, String> paramMap = APIConfig.getHeaderPar() != null ? (Map<String, String>)APIConfig.getHeaderPar().clone(): new LinkedHashMap<String, String>();
        //TODO -- 添加参数
        if(params != null && params.size() > 0) {
            paramMap.putAll(params);
        }

        Map<String, String> headerMap = new HashMap<>();
        String token = SystemContext.getInstance().getToken();
        String userId = SystemContext.getInstance().getUserId();
        headerMap.put("token", token);
        headerMap.put("userid", userId);

        String returnString = null;
        if(OKHttpUtil.isNetworkAvailable(SystemContext.getInstance().getContext())){
            try {
                String urlStr = modifyUserInfoApiUrl();
                returnString = OKHttpUtil.downWithPostJsonMethod(urlStr, paramMap, headerMap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //若返回为null,则是网络错误
        if(returnString == null || returnString.equalsIgnoreCase("")) {
            return null;
        } else {
            CommonInfo resultInfo = null;
            try {
                resultInfo = new Gson().fromJson(returnString, CommonInfo.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(resultInfo == null || CommonErrorHandler.getInstance().isError(resultInfo.getStatus(), resultInfo.getMessage())) {
                return null;
            } else {
                return resultInfo;
            }
        }
    }

    //获取用户地址列表
    public List<UserAddressInfo> getUserAddressList(Map<String, String> params) {
        Map<String, String> paramMap = APIConfig.getHeaderPar() != null ? (Map<String, String>)APIConfig.getHeaderPar().clone(): new LinkedHashMap<String, String>();
        //TODO -- 添加参数
        if(params != null && params.size() > 0) {
            paramMap.putAll(params);
        }
        Map<String, String> headerMap = new HashMap<>();
        String token = SystemContext.getInstance().getToken();
        String userId = SystemContext.getInstance().getUserId();
        headerMap.put("token", token);
        headerMap.put("userid", userId);

        String returnString = null;
        if(OKHttpUtil.isNetworkAvailable(SystemContext.getInstance().getContext())){
            try {
                String urlStr = userAddressListApiUrl();
                returnString = OKHttpUtil.downWithGetMethod(urlStr, paramMap, headerMap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //若返回为null,则是网络错误
        if(returnString == null || returnString.equalsIgnoreCase("")) {
            return null;
        } else {
            UserAddressResultInfo resultInfo = null;
            try {
                resultInfo = new Gson().fromJson(returnString, UserAddressResultInfo.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(resultInfo == null || resultInfo.getResponse() == null || CommonErrorHandler.getInstance().isError(resultInfo.getStatus(), resultInfo.getMessage())) {
                return null;
            } else {
                return resultInfo.getResponse();
            }
        }
    }

    //修改用户地址
    public CommonInfo modifyUserAddress(Map<String, String> params) {
        Map<String, String> paramMap = APIConfig.getHeaderPar() != null ? (Map<String, String>)APIConfig.getHeaderPar().clone(): new LinkedHashMap<String, String>();
        //TODO -- 添加参数
        if(params != null && params.size() > 0) {
            paramMap.putAll(params);
        }
        Map<String, String> headerMap = new HashMap<>();
        String token = SystemContext.getInstance().getToken();
        String userId = SystemContext.getInstance().getUserId();
        headerMap.put("token", token);
        headerMap.put("userid", userId);

        String returnString = null;
        if(OKHttpUtil.isNetworkAvailable(SystemContext.getInstance().getContext())){
            try {
                String urlStr = modifyUserAddressApiUrl();
                returnString = OKHttpUtil.downWithPostJsonMethod(urlStr, paramMap, headerMap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //若返回为null,则是网络错误
        if(returnString == null || returnString.equalsIgnoreCase("")) {
            return null;
        } else {
            CommonInfo resultInfo = null;
            try {
                resultInfo = new Gson().fromJson(returnString, CommonInfo.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(resultInfo == null || CommonErrorHandler.getInstance().isError(resultInfo.getStatus(), resultInfo.getMessage())) {
                return null;
            } else {
                return resultInfo;
            }
        }
    }

}
